package cn.iune.erp.invoice;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;

@Transactional
@Service
public class AccountInvoiceDetailServiceImpl implements IAccountInvoiceDetailService {
	
	@Autowired
	private IAccountInvoiceDetailDao dao;
	
	@Transactional
	@Override
	public void insert(AccountInvoiceDetail obj) {
		dao.insert(obj);
	}

	@Transactional
	@Override
	public void delete(String fID) {
		dao.delete(fID);
	}

	@Transactional
	@Override
	public void update(AccountInvoiceDetail obj) {
		dao.update(obj);
	}

	@Override
	public AccountInvoiceDetail getByID(String fID) {
		return dao.getByID(fID);
	}

	@Transactional
	@Override
	public int save(JSONObject object) throws Throwable {
		// TODO Auto-generated method stub
		// JSON转换Table
		Table table = Transform.jsonToTable(object);
		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
			AccountInvoiceDetail obj = JSON.toJavaObject(javaObject, AccountInvoiceDetail.class);
//			System.out.println(JSON.toJSONString(javaObject));
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				dao.update(obj);
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
			}
		}
		return 0;
	}

	
	@Override
	public List<AccountInvoiceDetail> getByMasterID(String fMasterID) {
		return dao.getByMasterID(fMasterID);
	}

	//通过入库单号，获取采购发票列表
	@Override
	public List<AccountInvoiceDetail> getByOrigin(String origin, String originNo, String originSerialNo, String enterpriseNo) {
		return dao.getByOrigin(origin, originNo, originSerialNo, enterpriseNo);
	}

	@Transactional
	@Override
	public int deleteByMasterID(String fMasterID){
		List<AccountInvoiceDetail> as = dao.getByMasterID(fMasterID);
		for(AccountInvoiceDetail a:as){
			dao.delete(a.getfID());
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int confirm(String fID) {
		AccountInvoiceDetail a = dao.getByID(fID);
		return this.confirm(a);
	}
	
	@Transactional
	@Override
	public int active(String fID) {
		AccountInvoiceDetail a = dao.getByID(fID);
		return this.active(a);
	}
	
	@Transactional
	@Override
	public int confirm(AccountInvoiceDetail a) {
		a.setState("confirm");
		a.setResultStatus("审核中");
		dao.update(a);
		return 0;
	}
	
	@Transactional
	@Override
	public int active(AccountInvoiceDetail a) {
		a.setState("active");
		a.setResultStatus("待审核");
		dao.update(a);
		return 0;
	}

	@Transactional
	@Override
	public int confirmByMasterID(String fMasterID){
		List<AccountInvoiceDetail> as = dao.getByMasterID(fMasterID);
		for(AccountInvoiceDetail a:as){
			this.confirm(a);
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int activeByMasterID(String fMasterID){
		List<AccountInvoiceDetail> as = dao.getByMasterID(fMasterID);
		for(AccountInvoiceDetail a:as){
			this.active(a);
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int open(String fID) {
		AccountInvoiceDetail a = dao.getByID(fID);
		return this.open(a);
	}
	
	@Transactional
	@Override
	public int close(String fID) {
		AccountInvoiceDetail a = dao.getByID(fID);
		return this.close(a);
	}
	
	@Transactional
	@Override
	public int open(AccountInvoiceDetail a) {
		a.setStatus("open");
		dao.update(a);
		return 0;
	}
	
	@Transactional
	@Override
	public int close(AccountInvoiceDetail a) {
		a.setStatus("close");
		dao.update(a);
		return 0;
	}

	@Transactional
	@Override
	public int openByMasterID(String fMasterID){
		List<AccountInvoiceDetail> as = dao.getByMasterID(fMasterID);
		for(AccountInvoiceDetail a:as){
			this.open(a);
		}
		return 0;
	}
	@Transactional
	@Override
	public int closeByMasterID(String fMasterID){
		List<AccountInvoiceDetail> as = dao.getByMasterID(fMasterID);
		for(AccountInvoiceDetail a:as){
			this.close(a);
		}
		return 0;
	}
	
	//审核通过，改变审核状态为confirm
	@Transactional
	@Override
	public int agreeIt(String fID) {
		//更新数据库工具入库单的单据状态（同时更新单据的结存数量、金额、件数）
		AccountInvoiceDetail a = dao.getByID(fID);
		return this.agreeIt(a);
	}
	//审核拒绝，改变审核状态为active
	@Transactional
	@Override
	public int unAgree(String fID) {
		//更新数据库工具入库单的单据状态
		AccountInvoiceDetail a = dao.getByID(fID);
		return this.unAgree(a);
	}	

	//审核通过，改变审核状态为confirm
	@Transactional
	@Override
	public int agreeIt(AccountInvoiceDetail a) {
		//更新数据库工具入库单的单据状态（同时更新单据的结存数量、金额、件数）
		
		a.setAudit("confirm");
		a.setConfirmDate(Utils.getCurrentDate());
		
		a.setResultStatus("已审核");
		dao.update(a);
		
		return 0;
	}
	
	//审核拒绝，改变审核状态为active
	@Transactional
	@Override
	public int unAgree(AccountInvoiceDetail a) {

		//更新数据库工具入库单的单据状态

		a.setAudit("active");
		a.setConfirmDate(null);
		
		a.setResultStatus("审核中");
		dao.update(a);

		return 0;
	}	


	@Transactional
	@Override
	public int agreeItByMasterID(String fMasterID){
		List<AccountInvoiceDetail> as = dao.getByMasterID(fMasterID);
		for(AccountInvoiceDetail a : as){
			this.agreeIt(a);
		}
		return 0;
	}
	
	@Transactional
	@Override
	public int unAgreeByMasterID(String fMasterID){
		List<AccountInvoiceDetail> as = dao.getByMasterID(fMasterID);
		for(AccountInvoiceDetail a : as){
			this.unAgree(a);
		}
		return 0;
	}
	
	
	
	
	
	@Transactional
	@Override
	public int updateEvaluateQuantityRinse(String accountInvoiceNo, String serialNo,String enterpriseNo,String audit){
		AccountInvoiceDetail obj = dao.getByNo(accountInvoiceNo, serialNo, enterpriseNo);
		if(audit.equals("confirm")) {
			obj.setEvaluateQuantityRinse(obj.getItemAmount());
			obj.setResultStatus("已归还");
		}else {
			obj.setEvaluateQuantityRinse(BigDecimal.ZERO);
			obj.setResultStatus("未归还");
		}
		dao.update(obj);
		return 0;
	}
	
	@Override
	public AccountInvoiceDetail packing(AccountInvoice accountInvoice){
		
		AccountInvoiceDetail obj = new AccountInvoiceDetail();
		String fID = Utils.getUUID();
		obj.setfID(fID);
		
		obj.setBillType(accountInvoice.getBillType());
		obj.setBillTypeName(accountInvoice.getBillTypeName());
		obj.setSystemCode(accountInvoice.getSystemCode());
		obj.setRevOpay(accountInvoice.getRevOpay());
		obj.setDirection(accountInvoice.getDirection());
		obj.setOrigin(accountInvoice.getOrigin());
		obj.setOriginName(accountInvoice.getOriginName());
		
//		obj.setFactoryID(accountInvoice.getFactoryID());
//		obj.setFactoryNo(accountInvoice.getFactoryNo());
//		obj.setFactoryName(accountInvoice.getFactoryName());
		
		obj.setState(accountInvoice.getState());
		obj.setStatus(accountInvoice.getStatus());
		obj.setAudit(accountInvoice.getAudit());
		obj.setOrderDate(accountInvoice.getOrderDate());
		
		obj.setItemAmount(BigDecimal.ZERO);
		obj.setOrderAmount(BigDecimal.ZERO);
		obj.setOrderAmountOld(BigDecimal.ZERO);
		obj.setOrderAmountTax(BigDecimal.ZERO);
		
		obj.setOrderAmountTotal(BigDecimal.ZERO);
		obj.setDiscountRate(BigDecimal.ONE);

		obj.setfMasterID(accountInvoice.getfID());
		obj.setAccountInvoiceNo(accountInvoice.getAccountInvoiceNo());
		//子表明细号
		int maxDetailNo = accountInvoice.getMaxDetailNo() + 1;
		String serialNo = String.format("%05d", maxDetailNo);
		obj.setSerialNo(serialNo);
		
		obj.setDealObject(accountInvoice.getDealObject());
		obj.setDealObjectLabel(accountInvoice.getDealObjectLabel());
		obj.setCustomerID(accountInvoice.getCustomerID());
		obj.setCustomerNo(accountInvoice.getCustomerNo());
		obj.setShortName(accountInvoice.getShortName());
		obj.setCustomerName(accountInvoice.getCustomerName());
		
		obj.setTaxType(accountInvoice.getTaxType());
		obj.setTaxTypeLabel(accountInvoice.getTaxTypeLabel());
		obj.setTaxRate(accountInvoice.getTaxRate());
		
		obj.setOrderNo(accountInvoice.getOrderNo());
		
		obj.setUserId(accountInvoice.getUserId());
		obj.setEnterpriseNo(accountInvoice.getEnterpriseNo());
		obj.setEnterpriseName(accountInvoice.getEnterpriseName());
		
		obj.setVersion(0);
		
		return obj;
	}


}
